package com.powernode.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.EsDomin.ProdEs;
import com.powernode.domain.Prod;
import com.powernode.service.SearchService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 从es中进行商品查询
     *
     * @param current
     * @param size
     * @param prodName
     * @param sort
     * @return
     */
    @Override
    public Page<ProdEs> ResponseEntity(Integer current, Integer size, String prodName, Integer sort) {
        Page<ProdEs> page = new Page<>(current, size);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //分页
        builder.withPageable(PageRequest.of(current - 1, size));

        //排序 根据sort sort=0 为综合 1为效率 2位价格
        builder.withSort(sort(sort));

        //关键字搜索  根据prodName和brief
        //如果没有搜索条件进行全局
        if (StringUtils.hasText(prodName)) {
            builder.withQuery(QueryBuilders.multiMatchQuery(prodName, "brief", "prodName"));
        }
        NativeSearchQuery query = builder.build();
        SearchHits<ProdEs> search = elasticsearchRestTemplate.search(query, ProdEs.class);
        long totalHits = search.getTotalHits();
        page.setTotal(totalHits);
        List<ProdEs> prodEs = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        page.setRecords(prodEs);
        return page;
    }

    /**
     * 标签对应的商品
     *
     * @param current
     * @param size
     * @param tagId
     * @return
     */
    @Override
    public Page<ProdEs> prodListByTagId(Integer current, Integer size, Long tagId) {
        Page<ProdEs> page = new Page<>(current, size);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //分页
        builder.withPageable(PageRequest.of(current - 1, size));
        //根据tagId查询
        builder.withQuery(QueryBuilders.matchQuery("tagList", tagId));

        NativeSearchQuery query = builder.build();
        SearchHits<ProdEs> search = elasticsearchRestTemplate.search(query, ProdEs.class);
        long totalHits = search.getTotalHits();
        page.setTotal(totalHits);
        List<ProdEs> prodEs = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        page.setRecords(prodEs);
        return page;
    }

    @Override
    public Page<ProdEs> pageProd(Long cateGoryId) {
        Page<ProdEs> page = new Page<>();
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //分页
        builder.withPageable(PageRequest.of(0, 10));
        //prodId
        //数据库对应 cateGoryId 对应商品的prodId 所以需要对应查询的是prodId
        builder.withQuery(QueryBuilders.matchQuery("prodId", cateGoryId));

        NativeSearchQuery query = builder.build();
        SearchHits<ProdEs> search = elasticsearchRestTemplate.search(query, ProdEs.class);
        long totalHits = search.getTotalHits();
        page.setTotal(totalHits);
        List<ProdEs> prodEs = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        page.setRecords(prodEs);
        return page;
    }

    /**
     * sort=0 为综合 1为效率 2位价格
     *
     * @param sort
     * @return
     */
    private SortBuilder sort(Integer sort) {
        switch (sort) {
            case 0:
                return new FieldSortBuilder("positiveRating").order(SortOrder.DESC);
            case 1:
                return new FieldSortBuilder("soldNum").order(SortOrder.DESC);
            case 2:
                return new FieldSortBuilder("price").order(SortOrder.DESC);
            default:
                return null;
        }
    }
}
